package com.ssm.service.impl;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;

import com.hbase.utils.DataSourceContextHolder;
import com.hbase.utils.DataSourceType;
import com.sitech.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import com.ssm.dao.UserMapper;
import com.ssm.pojo.User;
import com.ssm.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.hbase.ApplicationConfig.*;
import static com.hbase.utils.RandomUtil.testMathRandom10000;
import static com.hbase.utils.RandomUtil.testTable;

@Service  
public class UserServiceImpl implements UserService{  
  
    @Resource  
    private UserMapper userMapper;  
      
    @Override
    public User getUserById(String userId) {
        int i=testMathRandom10000();

        Integer rootApp=Integer.parseInt(ROOT_APP);
        Integer database1=Integer.parseInt(DATABASE1);
        Integer database2=0;
        if(!StringUtils.isEmptyOrNull(DATABASE2)){
            database2=Integer.parseInt(DATABASE2);
        }
        if(i%(database1+database2)<database1){
            DataSourceContextHolder.setDbType(DataSourceType.SOURCE_1);
        }else{
            DataSourceContextHolder.setDbType(DataSourceType.SOURCE_2);
        }

        if(i%SLOW_SQL_RATE==0){
            Map map=new HashMap(1);
            map.put("tableName",testTable(i%4));
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            map.put("uuid",uuid);
            this.userMapper.selectSlowSql(map);
        }else{
            this.userMapper.selectByPrimaryKey(userId);
        }

//        Map map=new HashMap(1);
//        map.put("tableName",testTable(i%4));
//        this.userMapper.selectSlowSql(map);

        return new User();
    }

    @Override
    public User selectByPhone(String phone) {

        return this.userMapper.selectByPhone(phone);
    }

    @Override
    public List<Map> selectSlowSql(String phone) {

        Map map=new HashMap(1);
        map.put("tableName",testTable(1));
        return userMapper.selectSlowSql(map);
    }

}  